<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Class will make all the events
 *
 * @author pleblanc
 */
class Qp_Event {

    private static $_allEvents = array();

    public static function addAction($classToHookTo, $hookName, array $classAndFunction, array $args = array(), $priority = 10)
    {
        $hookId = self::_createId($classToHookTo, $hookName, $classAndFunction, $priority);

        self::$_allEvents[$classToHookTo][$hookName][$priority][$hookId]['func'] = $classAndFunction;
        self::$_allEvents[$classToHookTo][$hookName][$priority][$hookId]['args'] = $args;

        return true;

    }
    public static function addFilter($classToHookTo, $hookName, array $classAndFunction, array $args = array(), $priority = 10)
    {
        return self::addAction($classToHookTo, $hookName, $classAndFunction, $args, $priority);
    }
    /**
     *
     * @param string $hookName Name of the hook used in the class
     * @param array $args an array of optional arguments to pass to the callback
     * @param string $classRequestingHook should only be the current class to which the hook is called. Not recommended to use this option.
     */
    public static function doAction($hookName, $args = array(), $classRequestingHook = '')
    {

        $temp = debug_backtrace();
        $classRequestingHook = $classRequestingHook == '' ? $temp[1]['class'] : $classRequestingHook;

        if(is_null(self::$_allEvents[$classRequestingHook][$hookName]))
            return;
        $sortedHooks = self::$_allEvents[$classRequestingHook][$hookName];
        
        ksort($sortedHooks);

        foreach ($sortedHooks as $priorities) {

            foreach($priorities as $uid){
                call_user_func_array($uid['func'], array_merge($uid['args'], $args));
            }
        }

        return true;

    }
    public static function doFilter($hookName, $toFilter, $additionalArgs = array(), $classRequestingHook = '')
    {
        $temp = debug_backtrace();
        $classRequestingHook = $classRequestingHook == '' ? $temp[1]['class'] : $classRequestingHook;

        if(is_null(self::$_allEvents[$classRequestingHook][$hookName]))
            return;
        $sortedHooks = self::$_allEvents[$classRequestingHook][$hookName];

        ksort($sortedHooks);

        array_unshift($additionalArgs,$toFilter);
        $outputValue = $toFilter;
        
        foreach ($sortedHooks as $priorities) {

            foreach($priorities as $uid){
                $additionalArgs[0] = $outputValue;
                $outputValue = call_user_func_array($uid['func'], array_merge($additionalArgs, $uid['args']));
            }
        }

        return $outputValue;
    }

    
    private static function _createId($classToHookTo, $hookName, array $classAndFunction, $priority)
    {
        return md5($classToHookTo.$hookName.$classAndFunction[0].$classAndFunction[1].$priority);
    }
}


Qp_Event::doAction('helloworld', array('blablabla', 'lulululu'));


?>
